package cn.com.open.itembank.mapper;

import cn.com.open.itembank.dto.TestPaperSearchDto;
import cn.com.open.itembank.entity.*;
import cn.com.open.itembank.sqlprovider.TestPaperReadSqlProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.Date;
import java.util.List;

/**
 * Created by lucky on 2017/10/30.
 */
@Mapper
public interface PaperReadMapper {

    //region 根据试卷id和题库id获取题库试卷关系

    /**
     * 根据试卷id和题库id获取题库试卷关系.
     *
     * @param itemBankId 题库id
     * @param id         试卷id
     * @return 题库和试卷关系 question test paper relation
     */
    @Select("SELECT * FROM TestPaper WHERE CourseID = #{itemBankId} AND TestPaperID = #{id}")
    TestPaper getQuestionTestPaper(@Param("itemBankId") String itemBankId, @Param("id") String id);

    /**
     * 根据试卷id获取试卷内容.
     *
     * @param id 试卷id
     * @return 题库和试卷关系 question test paper relation
     */
    @Select("SELECT * FROM TestPaperContent WHERE TestPaperID = #{id}")
    TestPaperContent getQuestionTestPaperContent(@Param("id") String id);

    //endregion

    //region 分页获取试卷信息

    /**
     * 分页获取试卷信息.
     *
     * @param testPaperSearchDto the test paper search dto
     * @return the paper list
     */
    @SelectProvider(type = TestPaperReadSqlProvider.class, method = "getTestPaperQuery")
    List<TestPaper> getPaperList(@Param("TestPaperSearchDto") TestPaperSearchDto testPaperSearchDto);

    //endregion

    //region 获取曝光信息表的信息

    /**
     * 获取曝光信息表的信息.
     *
     * @param id 试卷id
     * @return examination paper information
     */
    @Select("SELECT * FROM QuestionDisplayInfo WHERE TestPaperID = #{id}")
    List<QuestionDisplayInfo> getQuestionDisplayInfo(@Param("id") String id);

    //endregion

    //region 查询最近一次曝光时间

    /**
     * 查询最近一次曝光时间.
     *
     * @param id         试卷id
     * @param questionId 试题id
     * @return examination paper information
     */
    @Select("SELECT MAX(DisplayTime) FROM QuestionDisplayInfo WHERE TestPaperID <> #{id} AND QuestionID = #{questionId}")
    Date getMaxQuestionDisplayTime(@Param("id") String id, @Param("questionId") String questionId);

    /**
     * 获取试卷内容信息
     *
     * @param id 试卷id
     * @return test paper content
     */
    @Select("SELECT * FROM TestPaperContent WHERE TestPaperID = #{id}")
    TestPaperContent getTestPaperContent(@Param("id") String id);

    /**
     * 根据试卷和试题id获取曝光信息
     *
     * @param id         试卷id
     * @param questionId 试题id
     * @return question display by paper
     */
    @Select("SELECT * FROM QuestionDisplayInfo WHERE TestPaperID = #{id} AND QuestionID = #{questionId}")
    QuestionDisplayInfo getQuestionDisplayByPaper(@Param("id") String id, @Param("questionId") String questionId);

    //endregion

    //region 获取试卷复制用试卷信息
    /**
     * 获取试卷复制用试卷信息.
     *
     * @param itemBankId   the item bank id
     * @param testPaperIds the test paper ids
     * @return the papers for copy
     */
    @Select("SELECT * FROM TestPaper WHERE CourseID = #{itemBankId} AND TestPaperID IN (${testPaperIds}) AND State<>-1")
    List<TestPaper> getPapersForCopy(@Param("itemBankId") String itemBankId, @Param("testPaperIds") String testPaperIds);

    /**
     *
     * @param id
     * @param isPublish
     * @return
     */
    @Select("SELECT * FROM TestPaper WHERE TestPaperID = #{id} AND IsPublish = ${isPublish}")
    TestPaper getTestPaperIsPublish(@Param("id") String id, @Param("isPublish") int isPublish);


    /**
     * 查询count
     * @param testPaperSearchDto
     * @return
     */
    @SelectProvider(type = TestPaperReadSqlProvider.class, method = "getTestPaperCount")
    int getCount(@Param("TestPaperSearchDto") TestPaperSearchDto testPaperSearchDto);

    /**
     * 查询题库是否存在
     * @param itemBankId
     */
    @Select("select * from CourseItemBank where ItemBankId = #{itemBankId} AND Status <> -1")
    CourseItemBank CourseItemBank(@Param("itemBankId") String itemBankId);

    /**
     * 根据题库id获取题库下所有试卷id
     * @param itemBankId
     * @return
     */
    @Select("select TestPaperID from TestPaper where CourseID = #{itemBankId} AND State = 0 AND IsPublish = 1")
    List<String> getTestPaperIDById(String itemBankId);

    /**
     * 根据试卷id获取试卷xml
     * @param testPaperId
     * @return
     */
    @Select("select * from TestPaperContent where TestPaperID = #{testPaperId}")
    TestPaperContent getTestPaperXML(@Param("testPaperId") String testPaperId);

    /**
     * 根据试题id获取试题xml
     * @param questionId
     * @return
     */
    @Select("select ContentXML from QuestionContent where QuestionID = #{questionId}")
    String getQuestionContent(@Param("questionId") String questionId);
    //endregion
}
